<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<div>div</div>
<p>p</p>
<div>div</div>
<span>span</span>
<p>p</p>
<div>div</div>
</body>
<script>
    // 选择
    function select ( selector ) {
        return document.querySelectorAll( selector );
    };
    // 数组检测
    function isArrLike( obj ) {
        if ( obj.prototype.toString.call( obj ) == '[object Array]' ) {
            return true;
        }
        var length = 'length' in obj && obj.length;
        return typeof length === 'number' && length >= 0;
    }
    // 循环
    function each( arr, callback ) {
        if ( isArrayLike ( arr ) ) {
            for ( var i = 0; i < arr.length; i++ ) {
                if ( callback.call( arr[ i ], arr[ i ], i ) === false ) break;
            }
        } else {
            for ( var k in arr ) {
                if ( callback.call( arr[ k ], arr[ k ], k ) === false ) break;
            }
        }
        return arr;
    }
    // map 方法
    function map( arr, callback ) {
        var newArray = [], tmp;
        if ( isArrLike ( arr ) ) {
            for ( var i = 0; i < arr.length; i++ ) {
                tmp = callback( arr[ i ], i );
                if ( tmp != null ) {
                    newArray.push( tmp );
                }
            }
        } else {
            for ( var k in arr ) {
                tmp = callback( arr[ k ], k );
                if ( tmp != null ) {
                    newArr.push( tmp );
                }
            }
        }
        return newArr;
    }

    // 引入构造函数 F
    function F ( selector ) {
        [].push.apply( this, select( selector ) );
    }
    F.prototype.each = function ( callback ) {
        return each( this, callback );
    }

    var obj = new F( 'p, div' );
    obj.each( function () {
        this.style.border = '1px dashed red';
    });





















</script>
</html>